<?php
//URL CREATE FUNCTION
function video_url_item($id,$alias){
	return HOME."video/$id/$alias.html";
}
function video_url_user($username){
	return HOME."video/user/$username";
}
function video_url_category($id,$alias){
	return HOME."video/c$id/$alias";
}
function video_url_subcategory($id,$alias){
	return HOME."video/s$id/$alias";
}
function video_url_tag($alias){
	return HOME."video/tag/$alias";
}
//End url video function

function video_get_infor($url){
		$query_string = array();
		parse_str(parse_url($url, PHP_URL_QUERY), $query_string);
		$data['v']=$query_string["v"];
		$data['list']=$query_string["list"];
		$data['image']=	"http://i.ytimg.com/vi/".$data['v']."/default.jpg";
		
		
			if($_SESSION['mobile'] == 0) { $height = '360'; }else{ $height = '315';}
			if($data['list']){
				$data['embed'] = '<iframe id="ytvideo_'.$data['v'].'" width="100%" class="embed_video youtube_video" height="350" src="//www.youtube.com/embed/'.$data['v'].'?list='.$data['list'].'&enablejsapi=1" frameborder="0" allowfullscreen></iframe>';
			
			}else{
					$data['embed'] = '<iframe id="ytvideo_'.$data['v'].'" width="100%" class="embed_video youtube_video" height="350" src="//www.youtube.com/embed/'.$data['v'].'?enablejsapi=1" frameborder="0" allowfullscreen></iframe>';
				
			}
		
		return $data;
}

function video_get_category($id,$sub=''){
		$db=new db;
		$db->connect();
		// Main category
		$db->query("select * from blog where id=".$id);
		if($db->num_rows()>0){
				$dt['main']=$db->fetchOne();
				$dt['main']['url']=video_url_category($dt['main']['id'],$dt['main']['alias']);
		}
		//Subcategory
		if($sub!=''){
				$db->query("select * from blog where id=".$sub);
				if($db->num_rows()>0){
					$dt['sub']=$db->fetchOne();
					$dt['sub']['url']=video_url_subcategory($dt['sub']['id'],$dt['sub']['alias']);
				}
		}else{
				$db->query("select * from blog where parent=".$id);
				if($db->num_rows()>0){
					$re=$db->fetchAll();
					foreach($re as $val){
						
						$val['url']=video_url_subcategory($val['id'],$val['alias']);
						$dt['sub'][]=$val;
					}
				}
		}
		$db->close();
		return $dt;
}

function video_add_tag_xcrud($id,$str){
		
			if($str){
					
						$db=new db;
						$db->connect();
							$tagar=explode(",",$str);
										foreach($tagar as $value ){
											$value=trim($value);
											$tag=khongdau($value);
											if($tag){
											$db->query("select * from blog_tag where type='video' and alias ='".$tag."'");
													if($db->num_rows()>0){
															$row=$db->fetchOne();
														 	$db->nonquery("update blog_tag set num=num+1 where id=".$row['id']);
														}else{
															$db->nonquery("insert into blog_tag(name,alias,type,username) values('".$value."','".$tag."','video','".$_SESSION['user']['username']."')");
														}
											}// Neu tag ko rong
										}
										
						$db->close();
							
				}

}


function video_get_all_category(){
	$db=new db;
	$db->connect();
	$db->query("select id,name,alias from blog where type='video' and parent is NULL order by thutu asc");
	$val=$db->fetchAll();
	foreach($val as $row){
		$row['url']=video_url_category($row['id'],$row['alias']);
		$db->query("select id,name,alias from blog where type='video' and parent=".$row['id']." order by thutu asc");
		if($db->num_rows()>0){
			$val2=$db->fetchAll();
			foreach($val2 as $row2){
				$row2['url']=video_url_subcategory($row2['id'],$row2['alias']);
				$row['sub'][]=$row2;
			}
			
		}
		$data[]=$row;
	}
	
	return $data;
}

?>